import Vue from "vue";
import VueRouter from "vue-router";

Vue.use(VueRouter);
import home from '@/pages/home';
import search from '@/pages/search';
import register from '@/pages/register';
import login from '@/pages/login';


let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        //代表真:代表着两个形参接受参数【箭头函数】
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => { }, () => { });
    }
}

VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        //代表真:代表着两个形参接受参数【箭头函数】
        originReplace.call(this, location, resolve, reject);
    } else {
        originReplace.call(this, location, () => { }, () => { });
    }
}

export default new VueRouter({
  mode: 'history',
  routes: [
    {
      path: '/',
      redirect: '/home'
  },
  {
    path: '/home',
    name: 'Home',
    component: home,
    //路由元信息,新学习的一个配置项!!!!给当前路由添加一些额外数据
    //它的右侧是一个对象[可以有多个键值对]
    //路由配置项：书写的时候不要胡写、乱写、瞎写【在VC组件身上获取不到,没有任何意义】
    meta: { show: true },
     },
    {
    path: '/register',
    name: 'Register',
    component:register,
    meta: { show: false },
      } ,
    {
    path: '/login',
    name: 'Login',
    component: login,
    meta: { show: false },
      } ,
    {
      //接收params参数
      path: '/search/:keyword?',
      name: 'Search',
      component: search,
      meta: { show: true },
    //  新增配置项:props,给路由组件传递props参数
        //第一种布尔模式,相当于把params参数，作为props属性值传递给这个路由组件
        // props:true,

        //第二种:对象形式
        // props:{a:1,b:'我爱你'}

        //第三种写法:函数写法.一般是把query参数与params参数当中props传递给路由组件!!!
        //route就是当前路由
        // props:(route)=>{
        //      //是将当前箭头函数返回结果，作为props传递给search路由组件!!!
        //      return {a:route.params.keyword,b:'可以传递参数',c:route.query.keyword};
        // }
        // props:(route)=>({a:route.params.keyword,b:'可以传递参数',c:route.query.keyword
        //  }
        // )
      } 
    ] 
  }
)

